/*
 * jQuery File Upload Plugin JS Example
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * https://opensource.org/licenses/MIT
 */

/* global $, window */

var maxCount = 3;
var counter = 0;
$(function() {
	'use strict';
	// Initialize the jQuery File Upload widget:
	$('#fileupload').fileupload( {
		dataType : 'json',
		autoUpload : false,
		acceptFileTypes : /(\.|\/)(gif|jpe?g|png)$/i,
		maxNumberOfFiles : 1,
		maxFileSize : 1000000, // 1M
		// Uncomment the following to send cross-domain cookies:
		// xhrFields: {withCredentials: true},
		// url: 'http://127.0.0.1:8080/upload/upload.do'
		done : function(e, data) {
			var objs = data.result;
			for ( var i in objs) {
				var obj = objs[i];
				// alert(JSON.stringify(obj));
		var name = String(obj.name);
		var index = name.indexOf("_");
		var imgName = name.substring(index + 1);
		var num = imgName.indexOf(".");
		var id = imgName.substring(0, num) + "\\" + imgName.substring(num);
		$("#" + id).val(obj.id);
	}
	// data.context.text('Upload finished.');
	}
	}).bind('fileuploadadd', function(e, data) {
		var fileCount = data.files.length;
		var count =$("#uploadCount").val();
		if(typeof(count)!="undefined"){ 
			maxCount = count; 
		} 
		counter++;
		if (counter > maxCount) {
			layer.msg("上传图片不能超过"+maxCount+"张！");
			return false;
		}
	});

	// Enable iframe cross-domain access via redirect option:

	// Load existing files:
	$('#fileupload').addClass('fileupload-processing');
	$.ajax( {
		// Uncomment the following to send cross-domain cookies:
		// xhrFields: {withCredentials: true},
		url : 'http://127.0.0.1:8080/upload/upload.do',
		dataType : 'json',
		context : $('#fileupload')[0]
	}).always(function() {
		$(this).removeClass('fileupload-processing');
	}).done(function(result) {
		// $(this).fileupload('option', 'done')
			// .call(this, $.Event('done'), {result: result});
		});

});
function jsonpCallback(data) //回调函数
{
}
function deletePic(obj, id) {
	var num = id.indexOf(".");
	var pid = id.substring(0, num) + "\\" + id.substring(num);
	var imgId = $("#" + pid).val();
	var url = "http://127.0.0.1:8080/upload/delete.do";
	if (imgId.length > 10) {
		$.ajax( {
			type : "get",
			async : false,
			url : url,
			cache : false,
			dataType : "text",
			data : {imgId : imgId},
			success : function(data) {
				if(data == "success"){
					counter--;
					layer.msg("删除成功！");
					$(obj).parent().parent().remove();
				}else{
					layer.msg("删除失败！");
				}
			}
		});
	}
	
}
